package my_a10bbc.cn.xp.com.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import my_a10bbc.ContentValue;
import my_a10bbc.cn.xp.com.adapter.IIndexLVAdapter;
import my_a10bbc.cn.xp.com.bean.IIndexBean;
import my_a10bbc.cn.xp.com.bean.IIndexThirdBean;
import sdstcom.lbq.one.R;

/**
 * Created by Administrator on 2017/1/9.
 */
public class IIndexVpFragment extends Fragment implements OnScrollListener{
    private PullToRefreshListView lv;
    private ArrayList<IIndexThirdBean> list;
    private IIndexLVAdapter adapter;
    private LayoutInflater inflater;

    private ImageView ivfoot;
    private CallBacksss callBack;

    public static IIndexVpFragment getInstance(String type){
        Bundle bundle=new Bundle();
        bundle.putString("type",type);
        IIndexVpFragment fragment=new IIndexVpFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

public  void getcallback(CallBacksss callBack)
{
    this.callBack=callBack;
}


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.frag_frag_index,container,false);
        lv=(PullToRefreshListView)view.findViewById(R.id.lv_frag_frag_index);
//        init();

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    private void init() {

        inflater=LayoutInflater.from(getActivity());
        list=new ArrayList<>();
        adapter=new IIndexLVAdapter(getActivity(),list);

        View footview=inflater.inflate(R.layout.item_foot_index,null,false);
        ivfoot=(ImageView)footview.findViewById(R.id.iv_item_foot_index);

        AbsListView.LayoutParams params=
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,400);
        footview.setLayoutParams(params);



        final String type=getArguments().getString("type");
        RequestParams params1=new RequestParams(ContentValue.URL_BASE+type+ContentValue.URL_LAST);
        params1.setConnectTimeout(3000);
        x.http().get(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                IIndexBean obj=new Gson().fromJson(result,IIndexBean.class);
                list.addAll(obj.data.content_list);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        //添加底视图
        lv.getRefreshableView().addFooterView(footview);
        footview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sendData(type);
            }
        });



        lv.setAdapter(adapter);
        ivfoot.setImageResource(R.mipmap.abc_index_button_more);

//        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {


    }


    public interface CallBacksss{
        void  sendData(String s);
    }

}
